<?php
declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class Table extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('table')
            ->setDescription('the table command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $tables = Db::query("show tables");

        // 获取数据库名称
        $dbConfig = config('database.connections.mysql');

        $field = 'Tables_in_'.$dbConfig['database'];
        $tables = array_column($tables, $field);

        // 过滤tables中的tk_tables数据
        $tables = array_filter($tables, function($table){
            return $table != 'tk_tables';
        });

        // 获取已经存在的表
        $exists = Db::table('tk_tables')->column('name');

        // 过滤已经存在的表
        $tables = array_filter($tables, function($table) use ($exists){
            return !in_array($table, $exists);
        });

        $data = [];
        foreach($tables as $table){
            $data[] = ['name' => $table];
        }
        Db::table('tk_tables')->insertAll($data);

    }
}
